home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / TownMaze.lha / TownMaze / src.lzh / closegates.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  3KB  |  106 lines

  1. /*
  2. ** closegates.c  Copyright 1991 Kent Paul Dolan,
  3. **               Mountain View, CA, USA 94039-0755
  4. **
  5. ** Written to satisfy an inquiry on USENet's rec.games.programmer newsgroup.
  6. ** May be freely used or modified in any non-commercial work.  Copyrighted
  7. ** only to prevent patenting by someone else.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "townmaze.h"
  12. #include "townproto.h"
  13.  
  14. #ifdef __STDC__
  15. void closegates()
  16. #else
  17. int closegates()
  18. #endif
  19. {
  20.   int gatesleft;
  21.   int chosengate;
  22.   int foundgate;
  23.   int gatewalk;
  24.   int possiblegates;
  25.  
  26.   int mazei, mazej;
  27.  
  28. #if PROGRESS
  29.   fprintf(stderr,"Close Gates ");
  30. #endif
  31.  
  32.   if (mazegates <= leftgates) return;
  33.  
  34.   gatesleft = mazegates;
  35.  
  36.   possiblegates = (mazeheight-1) + (mazewidth-1);
  37.  
  38. /*
  39. **  fprintf(stderr,
  40. **    "\nmazegates %d leftgates %d gatesleft %d possiblegates %d\n",
  41. **    mazegates,leftgates,gatesleft,possiblegates);
  42. */
  43.  
  44.   while (gatesleft > leftgates)
  45.   {
  46.     chosengate = RANDOM()%gatesleft;
  47.  
  48.     foundgate = 0;
  49.  
  50.     for (gatewalk = 0; gatewalk < possiblegates; gatewalk++)
  51.     {
  52.       if (gatewalk < (mazewidth-1)/2) /* Pick a side: top? */
  53.       {
  54.         mazei = 0;
  55.         mazej = 2 * gatewalk + 1;
  56.       }
  57.       else
  58.         if (gatewalk < ((mazewidth-1)/2 + (mazeheight-1)/2)) /* right? */
  59.         {
  60.           mazei = 2 * (gatewalk - (mazewidth-1)/2) + 1;
  61.           mazej = mazewidth-1;
  62.         }
  63.         else
  64. /*
  65. **  Thanks to Stefan M. Linnemann <crissl@rulcvx.LeidenUniv.nl> for picking
  66. **  up the two errors flagged beneath his user ID below, where I had "+ 1" and
  67. **  needed the "- 1"'s shown.  Again, it dumped core on other machines, ran
  68. **  OK on mine.  Sigh.  Stefan was kind enough to send the repair back as a
  69. **  patch, and then to patiently explain to me in a second note _why_ what I
  70. **  did was wrong.
  71. */
  72.           if (gatewalk < ((mazewidth-1) + (mazeheight-1)/2)) /* bottom? */
  73.           {
  74.             mazei = mazeheight-1;
  75.             mazej = 2 * (((mazewidth-1)/2)-                      /* crissl */
  76.                     (gatewalk-((mazewidth-1)/2 + (mazeheight-1)/2))) - 1;
  77.           }
  78.           else  /* left! */
  79.           {
  80.             mazei = 2 * (((mazeheight-1)/2)-                      /* crissl */
  81.                     (gatewalk-((mazewidth-1) + (mazeheight-1)/2))) - 1;
  82.             mazej = 0;
  83.           }
  84.  
  85. /*
  86. **      fprintf(stderr,
  87. **              "gatesleft %d chosengate %d foundgate %d gatewalk %d",
  88. **              gatesleft,chosengate,foundgate,gatewalk);
  89. **      fprintf(sdterr," mazei %d mazej %d %c\n",
  90. **              mazei,mazej, cmaze[mazei][mazej]);
  91. */
  92.  
  93.       if ((cmaze[mazei][mazej] == VDOOR) || (cmaze[mazei][mazej] == HDOOR))
  94.         if (foundgate < chosengate)
  95.           foundgate++;
  96.         else
  97.         {
  98.           cmaze[mazei][mazej] = WALL;
  99.           gatesleft--;
  100.           break;
  101.         }
  102.     }
  103.   }
  104.   return;
  105. }
  106.